package com.techfengs.test18;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Test {//{{{
	private static SAXReader sr = new SAXReader();

	public static void main (String[] args) throws DocumentException {//{{{
		// }}}
		// => 2. 读取xml文件，返回document对象 ------------------------------------------------------------ {{{ 
		File f = new File("/home/phoenix/workspace/TestBilibiliPro/files/students.xml");
		Document dom = sr.read(f);
		System.out.println(dom);
		// }}}
		// => 3. 获取根节点 ------------------------------------------------------------ {{{ 
		Element sEle = dom.getRootElement();
		// }}}
		// => 4. 获取根节点下面的子节点 ------------------------------------------------------------ {{{ 
		Iterator<Element> it = sEle.elementIterator();
		while ( it.hasNext() ) {
			// 4.1 获取子节点
			Element se = it.next();
			// 4.2 获取子节点的属性
			List<Attribute> attrs = se.attributes();
			for ( Attribute a : attrs ) {
				System.out.println("该子节点的属性" + a.getName() + "---" + a.getText());

			}
			// 4.3 获取子节点的子节点
			Iterator<Element> sit = se.elementIterator();
			while ( sit.hasNext() ) {
				Element e = sit.next();
				System.out.println("节点：" + e.getName() + "---" + e.getText());

			}
		}
		System.out.println();
		// }}}
	}//}}}

	private static void f(SAXReader sr, File f) throws DocumentException {
		sr.read(f);
	}
}
//}}}
